import {
  METHOD_TYPE,
  METHOD_TYPE_ACTION,
  METHOD_TYPE_GETTER,
  METHOD_TYPE_MUTATION,
  MODULE_NAME
} from './constant'
import type { ModuleDecoratorParams } from './type'

export function Module(params: ModuleDecoratorParams): ClassDecorator {
  return (target) => {
    const { name, store } = params
    if (!store) {
      throw new Error('Can not use module before create store.')
    }
    Reflect.defineMetadata(MODULE_NAME, name, target)
  }
}

export const Getter: MethodDecorator = (target, propertyKey, descriptor) => {
  Reflect.defineMetadata(METHOD_TYPE, METHOD_TYPE_GETTER, target, propertyKey)
}

export const Mutation: MethodDecorator = (target, propertyKey, descriptor) => {
  Reflect.defineMetadata(METHOD_TYPE, METHOD_TYPE_MUTATION, target, propertyKey)
}

export const Action: MethodDecorator = (target, propertyKey, descriptor) => {
  Reflect.defineMetadata(METHOD_TYPE, METHOD_TYPE_ACTION, target, propertyKey)
}
